package com.gree.scmservice.service.saas.impl;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.framework.entity.SaasTenant;
import com.gree.framework.entity.Smbse01;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.config.DataSourceContext;
import com.gree.scmservice.config.MultiRouteDataSource;
import com.gree.scmservice.dao.saasDao.SaasSiteDao;
import com.gree.scmservice.dao.saasDao.SaasTenantDao;
import com.gree.scmservice.entity.saas.SaasSite;
import com.gree.scmservice.entity.smbse.Smbse05;
import com.gree.scmservice.service.saas.SaasSiteService;
import com.gree.scmservice.util.UUIDUtil;
import org.nutz.dao.Chain;
import org.nutz.dao.Dao;
import org.nutz.dao.TableName;
import org.nutz.dao.impl.FileSqlManager;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * Created by 180686 on 2021/7/8 17:26
 */
@Service
public class SaasSiteServiceImpl implements SaasSiteService {

    @Autowired
    Dao dao;

    @Autowired
    SaasSiteDao saasSiteDao;

    @Autowired
    SaasTenantDao saasTenantDao;

    @Autowired
    private MultiRouteDataSource dataSource;

    @Override
    public ReturnVo createSite(SaasSite saasSite) {
        /*获取当前租户*/
        String username = SecurityUtils.getUserWithPasswordMode().get("username").toString();
        SaasTenant saasTenant = saasTenantDao.selectSaasSysTenantById(username);

        /*生成站点uuid*/
        String siteId = UUIDUtil.getNumberUUID();

        //*创建基础数据*//
        Dao dao4 = new NutDao(dataSource,new FileSqlManager("sql/smbse.sqls"));
        String[] keys4 = dao4.sqls().keys();
        for(String key : keys4) {
            Sql sql = dao4.sqls().create(key);
            sql.setVar("user",saasTenant.getClientId());
            sql.setVar("COMP",siteId);
            dao4.execute(sql);
        }

        /*Saas系统：添加站点基本信息*/
        saasSite.setCreate_time(LocalDateTime.now());
        saasSite.setSite_id(siteId);
        saasSite.setTenantId(saasTenant.getTenant_id());
        saasSiteDao.insertSaasSite(saasSite);

        /*切换到saas租户的数据库*/
        DataSourceContext.setDBType(saasTenant.getClientId());
        /*租户系统：添加站点基本信息*/
        dao.insert("smcom00",
                Chain.make("comp",siteId).add("dsca",saasSite.getTitle()).add("ctyp",""));

        /*租户系统：添加smbse01管理员租户信息 （admin:123456）*/
        Smbse01 sysUser = new Smbse01();
        sysUser.setUsid("admin");
        sysUser.setPawd("UXdlITIzNDU=");
        sysUser.setNama("管理员");
        LocalDateTime date = LocalDateTime.now();
        sysUser.setIndt(LocalDateTime.now());
        sysUser.setExdt(date.plusYears(1));
        TableName.set(siteId);
        dao.insert(sysUser);
        TableName.clear();


        /*租户系统：添加smbse05管理员角色*/
        Smbse05 smbse05 = new Smbse05();
        smbse05.setRoid("SAASAD");
        smbse05.setRtyp(1);
        smbse05.setPano("ADMIN");
        smbse05.setDsca("管理员");
        TableName.set(siteId);
        dao.insert(smbse05);
        TableName.clear();

        /*租户系统：添加smbse09管理员角色绑定*/
        dao.insert("smbse09"+siteId,
                Chain.make("usid","admin").add("roid","SAASAD"));

        return ReturnVo.success(siteId);
    }

    @Override
    public int updateSite(SaasSite saasSite) {
        return saasSiteDao.updateSaasSite(saasSite);
    }

    @Override
    public int updateStatus(SaasSite saasSite) {
        return saasSiteDao.updateSaasSiteStatus(saasSite.getSite_id(),saasSite.getStatus());
    }

    @Override
    public int deleteSiteByIds(String[] siteIds) {
        return saasSiteDao.deleteSaasSiteByIds(siteIds);
    }

    @Override
    public int deleteSiteByIdsOnUser(String[] siteIds) {
        Long tenantId = Long.valueOf(SecurityUtils.getUserWithPasswordMode().get("username").toString());
        return saasSiteDao.deleteSaasSiteByIdsOnUser(tenantId, siteIds);
    }

    @Override
    public TablePageData<SaasSite> selectSiteList(SaasSite saasSite) {
        TablePageData tablePageData = saasSiteDao.selectSaasSiteList(saasSite);
        return tablePageData;
    }

    @Override
    public TablePageData<SaasSite> selectSiteListOnUser(SaasSite saasSite) {
        String tenantId = SecurityUtils.getUserWithPasswordMode().get("username").toString();
        saasSite.setTenantId(tenantId);
        TablePageData tablePageData = saasSiteDao.selectSaasSiteList(saasSite);
        return tablePageData;
    }

    @Override
    public SaasSite selectSiteById(String siteId) {
        SaasSite saasSite = saasSiteDao.selectSaasSiteById(siteId);
        return saasSite;
    }


}
